/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.hotel.rest;

import com.alibaba.fastjson.JSON;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.app.ad.service.AduserXHotelService;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelQueryCriteria;
import me.zhengjie.modules.app.hotel.domain.HotelInfo;
import me.zhengjie.modules.app.hotel.service.HotelInfoService;
import me.zhengjie.modules.app.hotel.service.dto.HotelInfoNotInQueryCriteria;
import me.zhengjie.modules.app.hotel.service.dto.HotelInfoQueryCriteria;
import me.zhengjie.modules.system.service.DataService;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.utils.SecurityUtils;

import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用：酒店信息")
@RequestMapping("/api/hotelInfo")
public class HotelInfoController {

    private final HotelInfoService hotelInfoService;

    private final DeptService deptService;
    private final DataService dataService;
    private final UserService userService;

    private final AduserXHotelService aduserXHotelService;

    @Log("导出酒店信息")
    @ApiOperation("导出酒店信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hotelInfo:list')")
    public void download(HttpServletResponse response, HotelInfoQueryCriteria criteria) throws Exception {
    	DeptQueryCriteria deptCriteria = new DeptQueryCriteria();
    	List<DeptDto> deptList = deptService.queryAll(deptCriteria, true);
    	Set<Long> deptIds = deptList.stream().map(DeptDto::getId).collect(Collectors.toSet());
    	List<Long> depts = new ArrayList<Long>(deptIds);
    	for(Long deptId : depts) {
    		criteria.getDeptId().add(deptId+"");
    	}
        hotelInfoService.download(hotelInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询酒店信息")
    @ApiOperation("查询酒店信息")
    @PreAuthorize("@el.check('hotelInfo:list')")
    public ResponseEntity<Object> query(HotelInfoQueryCriteria criteria, Pageable pageable) throws Exception{
    	DeptQueryCriteria deptCriteria = new DeptQueryCriteria();
    	List<DeptDto> deptList = deptService.queryAll(deptCriteria, true);
//    	Set<Long> deptIds = deptList.stream().map(DeptDto::getId).collect(Collectors.toSet());
    	List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
    	if(criteria.getDeptId() == null) {
    		List<Long> depts = new ArrayList<Long>(dataScopes);
    		List<String> tempDepts = new ArrayList<String>();

        	for(Long deptId : depts) {
        		tempDepts.add(deptId + "");
        	}
        	criteria.setDeptId(tempDepts);
    	}
        return new ResponseEntity<>(hotelInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    @GetMapping("adUser")
    @Log("查询酒店信息")
    @ApiOperation("查询酒店信息")
    @PreAuthorize("@el.check('hotelInfo:list')")
    public ResponseEntity<Object> queryAdUser(HotelInfoNotInQueryCriteria criteria, Pageable pageable) throws Exception{
    	DeptQueryCriteria deptCriteria = new DeptQueryCriteria();
    	List<DeptDto> deptList = deptService.queryAll(deptCriteria, true);
//    	Set<Long> deptIds = deptList.stream().map(DeptDto::getId).collect(Collectors.toSet());
    	List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
    	if(criteria.getDeptId() == null) {
    		List<Long> depts = new ArrayList<Long>(dataScopes);
    		List<String> tempDepts = new ArrayList<String>();

        	for(Long deptId : depts) {
        		tempDepts.add(deptId + "");
        	}
        	criteria.setDeptId(tempDepts);
    	}

    	if(criteria.getUserId() == null) {
    		throw new BadRequestException("参数丢失");
    	}
    	AduserXHotelQueryCriteria aduserXHotelQueryCriteria = new AduserXHotelQueryCriteria();
		aduserXHotelQueryCriteria.setUserId(criteria.getUserId());

		List<AduserXHotelDto> aduserXHotels = aduserXHotelService.queryAll(aduserXHotelQueryCriteria);
		Set<String> hotelId = aduserXHotels.stream().map(AduserXHotelDto::getHotelId).collect(Collectors.toSet());

    	List<String> hotelIds = new ArrayList<String>(hotelId);

    	criteria.setIds(hotelIds);
        return new ResponseEntity<>(hotelInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增酒店信息")
    @ApiOperation("新增酒店信息")
    @PreAuthorize("@el.check('hotelInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody HotelInfo resources){
    	if(hotelInfoService.isExistByHotelNo(resources.getHotelNo())) {
    		throw new BadRequestException("酒店编号已存在");
    	}
        return new ResponseEntity<>(hotelInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改酒店信息")
    @ApiOperation("修改酒店信息")
    @PreAuthorize("@el.check('hotelInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody HotelInfo resources){
        hotelInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除酒店信息")
    @ApiOperation("删除酒店信息")
    @PreAuthorize("@el.check('hotelInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        hotelInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
